<template>
	<view class="container">
		<jc-container ref='container' @setPageConfig='setPageConfig'></jc-container>
		<!-- <jc-itemTagTabs ref='itemTagTabs'></jc-itemTagTabs> -->
		<jcTabbarItem :current="0" :list='dynamicTabbar'></jcTabbarItem>
	</view>
</template>

<script>
	import {
		common
	} from '@/api/common.js'
	import {
		page
	} from '@/api/page.js'
	import {
		shop
	} from '@/api/shop.js'
	import {
		update
	} from '@/common/utils/update.js'
	import lesseeCell from "@/components/lesseeCell/lesseeCell.vue"
	import {
		getLocation
	} from '@/common/utils/getLocation.js'
	import jcTabbarItem from '@/components/jc-tabbarItem/index.vue'
	import {
		tabbarItem
	} from "@/api/tabbarItem.js"
	export default {
		components: {
			lesseeCell,
			jcTabbarItem,
		},
		data() {
			return {
				pageConfig: {},
				pageConfigId: '',
				nodeList: [],
				tagList: [],
				guestList: [],
				clientInfo: {},
				page: 1,
				size: 20,
				showEmpty: false,
				isLastPage: false,
				noticeText: '',
				shopList: [],
				dynamicTabbar: [],
				tabsForShop: [],
				currentForShopTabs: 0, // tabs组件的current值，表示当前活动的tab选项
				currentAddr: {
					address: '定位中...'
				},
			}
		},
		onLoad(options) {
			if(this.$Route.query.pageConfigId){
				this.pageConfigId = this.$Route.query.pageConfigId
			}
			/**********************小程序普通二维码接收参数******************/
			if (options.q) {
				console.log('小程序普通二维码接收的参数', decodeURIComponent(options.q))
				getApp().globalData.sceneConfig = this.$tools.urlParmasToObj(decodeURIComponent(options.q))
			} else if (options) {
				getApp().globalData.sceneConfig = options
			}
			uni.showShareMenu({
				withShareTicket:true,
				//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus:["shareAppMessage","shareTimeline"]
			
			})
			
			var _this = this
			uni.$on('checkUpdate', function(data) {
				if (data.scene == 'mine') {
					_this.getAssetVersion(data.scene)
				}
			})
			this.getAssetVersion('home')
			this.getTabbarItemList()
			this.$refs.container.pageName = this.$Route.name
			this.$refs.container.loadData()
		},
		onShow() {
		},
		onUnload() {
			uni.$off('checkUpdate')
		},
		onPullDownRefresh() {
			this.getAssetVersion('home')
			this.getTabbarItemList()
			this.$refs.container.pageName = this.$Route.name
			this.$refs.container.pullDownRefresh()
			// this.$refs.itemTagTabs.pullDownRefresh()
		},
		onReachBottom() {
			if (this.isLastPage) {
				console.log('已无数据')
				return
			}
			console.log('上拉加载更多')
			this.$refs.container.reachBottom()
		},
		onShareAppMessage(res) {
			return {
				title: this.$store.getters.clientInfo.appname,
				path: 'pages/home/home',
				imageUrl: this.$store.getters.clientInfo.shareCoverUrl,
			}
		},
		methods: {
			tabsChange(index) {
				this.currentForShopTabs = index;
				this.page = 1
				this.getShopList()
			},
			getShopList() {
				var currentTab = this.tabsForShop[this.currentForShopTabs]
				var query = JSON.parse(currentTab.query)
				shop.getShopForCList({
					page: this.page,
					size: 20,
					scene: query.scene
				}).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if (this.page == 1) {
							this.shopList = []
						}
						var list = res.data.data.list
						this.shopList = [...this.shopList, ...list]
						this.showEmpty = this.shopList.length < 1
						if (res.data.data.next) {
							this.page += 1
							this.isLastPage = false
						} else {
							this.isLastPage = true
						}
					}
				})
			},
			setPageConfig(pageConfig){
				uni.stopPullDownRefresh()
				this.pageConfig = pageConfig
				uni.setNavigationBarColor({
					backgroundColor: this.pageConfig.background,
					frontColor: (!this.pageConfig.navigationBarBackground || this.pageConfig.navigationBarBackground =='#FFF' || this.pageConfig.navigationBarBackground =='#fff' || this.pageConfig.navigationBarBackground =='#FFFFFF' || this.pageConfig.navigationBarBackground =='#ffffff')  ? "" : "#ffffff"
				})
			},
			setSceneConfig(sceneConfig) {
				// console.log('sceneConfig', sceneConfig)
				switch (sceneConfig.CHN) {
					case 'regCode':
						uni.navigateTo({
							url: '/pages/login/reg?parentCode=' + sceneConfig.parentCode
						})
						break;
					case 'toLessee':
						this.$store.commit('lessee', sceneConfig.lessee)
						break;
					case 'shopHome':
						this.$Router.push({
							name: 'lesseeDetail',
							params: {
								shopId: sceneConfig.shop
							}
						})
						break;
					case 'shopReceivingQrcode':
						this.$Router.push({
							name: 'shopReceing',
							params: {
								shopId: sceneConfig.shop,
								shopName: sceneConfig.shopName,
								parentCode: sceneConfig.parentCode
							}
						})
						break;
					case 'coupon':
						this.$Router.push({
							name: 'businessCenter',
							params: {
								lesseeId: sceneConfig.shop,
								qrcode: sceneConfig.qrcode,
								CHN: 'coupon'
							}
						})
						break;
					default:
						this.$Router.push({
							name: sceneConfig.routerName,
							params: sceneConfig
						})
						break;
				}
			},
			getAssetVersion(scene) {
				common.findClientInfo().then(res => {
					if (res.data.code == 10000) {
						var clientInfo = res.data.data
						this.clientInfo = clientInfo
						this.$storage.setStorageDataSync(this.$store.state.clientname, clientInfo)
						this.$store.commit('clientInfo', clientInfo)
						// #ifdef APP-PLUS
						if (uni.getSystemInfoSync().model != 'iPhoneSimulator') {
							console.log('当前平台', uni.getSystemInfoSync())
							update.checkAppUpdate(clientInfo, scene)
						}
						// #endif
						
						if (getApp().globalData.sceneConfig) {
							this.setSceneConfig(getApp().globalData.sceneConfig)
						}
					}
				})
			},
			getTabbarItemList() {
				tabbarItem.getTabbarItemList({
					isShow: 1
				}).then(res => {
					if (res.data.code == 10000) {
						this.dynamicTabbar = res.data.data.list
						this.$store.commit('dynamicTabbar', this.dynamicTabbar)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		min-height: 100vh;
	}
</style>
